package com.gy.paidao.main.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.paidao.R;
import com.gy.paidao.main.bean.NotifyBean;
import com.gy.paidao.view.BaseSwipListAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author GY
 * @date 16/3/27
 * @Description:
 */
public class NotificationAdapter extends BaseSwipListAdapter {

    private Context context;
    private List<NotifyBean> list;

    public NotificationAdapter(Context context, List<NotifyBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if(list.size() > 0){
            return list.size() + 1;
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_notification_layout,null);
            holder.llMain = (LinearLayout) convertView.findViewById(R.id.adapter_notify_ll_main);
            holder.imgFlag = (ImageView) convertView.findViewById(R.id.adapter_notify_img_flag);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.adapter_notify_tv_title);
            holder.tvName = (TextView) convertView.findViewById(R.id.adapter_notify_tv_name);
            holder.tvTime = (TextView) convertView.findViewById(R.id.adapter_notify_tv_time);
            holder.tvContent = (TextView) convertView.findViewById(R.id.adapter_notify_tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(position == 0){
            holder.llMain.setVisibility(View.GONE);
            holder.tvTitle.setVisibility(View.VISIBLE);
            holder.tvTitle.setText(context.getResources().getString(R.string.notification_notify));
        }  else {
            if("comment".equals(list.get(position - 1).noticeType)){
                holder.imgFlag.setImageResource(R.mipmap.ic_notify_blue);
                holder.tvContent.setText(list.get(position - 1).content+"");
            } else {
                holder.imgFlag.setImageResource(R.mipmap.ic_notify_fire);
                holder.tvContent.setText(list.get(position - 1).description+"");
            }

            holder.tvName.setText(list.get(position - 1).title);
            String time = list.get(position - 1).createTime;
            if(!TextUtils.isEmpty(list.get(position - 1).createTime)){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat format1 = new SimpleDateFormat("MM-dd HH:mm");
                SimpleDateFormat format2 = new SimpleDateFormat("HH:mm");
                SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    String timeDay = format3.format(format.parse(time));
                    String timeToday = format3.format(new Date());
                    if(timeDay.equals(timeToday)){
                        time = format2.format(format.parse(time));
                    } else {
                        time = format1.format(format.parse(time));
                    }
                    holder.tvTime.setText(time);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }


            holder.llMain.setVisibility(View.VISIBLE);
            holder.tvTitle.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public boolean getSwipEnableByPosition(int position) {
        //是否可以划出
        if(position == 0){
            return false;
        }
        return true;
    }

    class ViewHolder{
        LinearLayout llMain;
        ImageView imgFlag;
        TextView tvTitle,tvContent,tvTime,tvName;
    }
}
